package com.example.theroster;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        LoadFromPrefs();
        SetSliderListeners();
    }


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    
    public void LoadFromPrefs() {
    	
    	SharedPreferences prefs = getPreferences( MODE_PRIVATE );
    	
    	EditText edtName = (EditText)findViewById(R.id.edtName);
    	edtName.setText( prefs.getString( "name", null ) );
    	
    	CheckBox chkSteady = (CheckBox)findViewById(R.id.chkSteady);
    	chkSteady.setChecked( prefs.getBoolean( "steady", false ) );
    	
    	Spinner spnEyeGlove = (Spinner)findViewById(R.id.spnEyeColour);
    	spnEyeGlove.setSelection( prefs.getInt( "eye", 0 ) );
    	
    	spnEyeGlove = (Spinner)findViewById(R.id.spnGloveSize);
    	spnEyeGlove.setSelection( prefs.getInt( "glove", 0 ) );
    	
    	DatePicker datBirthDate = (DatePicker)findViewById(R.id.datBirthDate);
    	int year = prefs.getInt( "year", Calendar.YEAR );
    	int month = prefs.getInt( "month", Calendar.MONTH );
    	int day = prefs.getInt( "day", Calendar.DAY_OF_MONTH );
    	
    	datBirthDate.updateDate( year, month, day );
  	
    	RadioGroup rgShirtSize = (RadioGroup)findViewById(R.id.rgShirtSize);
    	rgShirtSize.check( prefs.getInt( "shirt", 0 ) );
    	
    	SeekBar sldPantShoe = (SeekBar)findViewById(R.id.sldPantSize);
    	sldPantShoe.setProgress( prefs.getInt( "pant", 0 ) );
    	
    	sldPantShoe = (SeekBar)findViewById(R.id.sldShoeSize);
    	sldPantShoe.setProgress( prefs.getInt( "shoe", 0 ) );
    	
    	SetSliders();
    }


	public void SaveToPrefs( View v ) {
    	
    	SharedPreferences prefs = getPreferences( MODE_PRIVATE );
    	SharedPreferences.Editor prefsEdit = prefs.edit();
    	
    	EditText edtName = (EditText)findViewById(R.id.edtName);
    	prefsEdit.putString( "name", edtName.getText().toString() );
    	
    	CheckBox chkSteady = (CheckBox)findViewById(R.id.chkSteady);
    	prefsEdit.putBoolean( "steady", chkSteady.isChecked() );
    	
    	Spinner spnEyeGlove = (Spinner)findViewById(R.id.spnEyeColour);
    	prefsEdit.putInt( "eye", spnEyeGlove.getSelectedItemPosition() );
    	
    	spnEyeGlove = (Spinner)findViewById(R.id.spnGloveSize);
    	prefsEdit.putInt( "glove", spnEyeGlove.getSelectedItemPosition() );
    	
    	DatePicker datBirthDate = (DatePicker)findViewById(R.id.datBirthDate);
    	prefsEdit.putInt( "year", datBirthDate.getYear() );
    	prefsEdit.putInt( "month", datBirthDate.getMonth() );
    	prefsEdit.putInt( "day", datBirthDate.getDayOfMonth() );
  	
    	RadioGroup rgShirtSize = (RadioGroup)findViewById(R.id.rgShirtSize);
    	prefsEdit.putInt( "shirt",  rgShirtSize.getCheckedRadioButtonId() );
    	
    	SeekBar sldPantShoe = (SeekBar)findViewById(R.id.sldPantSize);
    	prefsEdit.putInt( "pant", sldPantShoe.getProgress() );
    	
    	sldPantShoe = (SeekBar)findViewById(R.id.sldShoeSize);
    	prefsEdit.putInt( "shoe", sldPantShoe.getProgress() );
    	
    	prefsEdit.commit();
    }
    
    
    private void SetSliders() {

    	TextView lblIndicator = (TextView)findViewById(R.id.lblPantIndicator);
    	SeekBar sldSlider = (SeekBar)findViewById(R.id.sldPantSize);
    	
    	lblIndicator.setText( String.valueOf( sldSlider.getProgress() + 16 ) );
    	
    	lblIndicator = (TextView)findViewById(R.id.lblShoeIndicator);
    	sldSlider = (SeekBar)findViewById(R.id.sldShoeSize);
    	
    	lblIndicator.setText( String.valueOf( sldSlider.getProgress() + 4 ) );
	}


    private void SetSliderListeners() {
		
    	SeekBar sldSlider = (SeekBar)findViewById(R.id.sldPantSize);
    	sldSlider.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {       

		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
		    	
		    	TextView lblIndicator = (TextView)findViewById(R.id.lblPantIndicator);
		    	SeekBar sldSlider = (SeekBar)findViewById(R.id.sldPantSize);
		    	
		    	lblIndicator.setText( String.valueOf( sldSlider.getProgress() + 16 ) );
		    }

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
    	});
    	
    	sldSlider = (SeekBar)findViewById(R.id.sldShoeSize);
    	sldSlider.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {       

		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
		    	
		    	TextView lblIndicator = (TextView)findViewById(R.id.lblShoeIndicator);
		    	SeekBar sldSlider = (SeekBar)findViewById(R.id.sldShoeSize);
		    	
		    	lblIndicator.setText( String.valueOf( sldSlider.getProgress() + 4 ) );
		    }

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
    	});
	}
    
}
